//
//  MZExtensionFont.swift
//  MZTool
//
//  Created by gby on 2022/1/25.
//

import Foundation

fileprivate var CUSTOM_FONT_NAME = ""//"PingFangSC-Regular"
fileprivate var CUSTOM_FONT_NAME_MID = ""//PingFangSC"
fileprivate var CUSTOM_FONT_NAME_BLOD = ""//PingFangSC-Semibold"
fileprivate var CUSTOM_FONT_NAME_Light = ""
fileprivate var CUSTOM_GBY_FONT_AUTO_CHANGE = false

internal func CUSTOM_SETTING_FONTSIZE_DEPENDONSCREEN(_ ofSize: CGFloat) -> CGFloat {
    guard CUSTOM_GBY_FONT_AUTO_CHANGE else {
        return ofSize
    }
    let windowheight = UIScreen.main.bounds.height as CGFloat
    let windowwidth = UIScreen.main.bounds.width as CGFloat
    var returnSize = ofSize
    if windowheight == 480.0 ||  windowheight == 568.0{
        returnSize -= 1
    }
    else if windowheight == 736.0 || (MZ.windowSafeAreaInsets().top > 0 && windowwidth >= 414.0 ){
        returnSize += 1
    }
    return returnSize
}

extension MZKit where Base == UIFont {
    public static func config(regular: String?, mid: String?, blod: String? , light: String?, auto: Bool = false){
        if let name = regular {
            CUSTOM_FONT_NAME = name
        }
        if let name = mid {
            CUSTOM_FONT_NAME_MID = name
        }
        if let name = blod {
            CUSTOM_FONT_NAME_BLOD = name
        }
        if let name = light {
            CUSTOM_FONT_NAME_Light = name
        }
        CUSTOM_GBY_FONT_AUTO_CHANGE = auto
    }
    
    public static func size(_ ofSize: CGFloat, weight: UIFont.Weight = .regular, auto: Bool? = nil) -> UIFont {
        let autochange = auto ?? CUSTOM_GBY_FONT_AUTO_CHANGE
        let fsize = autochange ? CUSTOM_SETTING_FONTSIZE_DEPENDONSCREEN(ofSize) : ofSize
        var cfont: UIFont?
        switch weight {
        case .regular:
            cfont = UIFont.init(name: CUSTOM_FONT_NAME, size: fsize)
        case .medium:
            cfont = UIFont.init(name: CUSTOM_FONT_NAME_MID, size: fsize)
        case .bold:
            cfont = UIFont.init(name: CUSTOM_FONT_NAME_BLOD, size: fsize)
        case .light:
            cfont = UIFont.init(name: CUSTOM_FONT_NAME_Light, size: fsize)
        default:
            break
        }
        return cfont ?? UIFont.systemFont(ofSize: fsize, weight: weight)
    }
}
